/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import CRUD.MemberHome;
import CRUD.ThreadHome;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.actions.DispatchAction;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;
import xmlMapping.Thread;
import xmlMapping.*;
import ModeratorOperations.*;
import beans.SplitActionForm;
import java.util.Date;

/**
 *
 * @author Mostafa
 */
public class SplitThreadAction extends DispatchAction {

    /* forward name="success" path="" */
    private final static String SUCCESS = "success";

    /**
     * This is the Struts action method called on
     * http://.../actionPath?method=myAction1,
     * where "method" is the value specified in <action> element : 
     * ( <action parameter="method" .../> )
     */
     public ActionForward Split_Threads(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        
        Integer id=  Integer.parseInt( request.getParameter("threadid"));
        System.out.println("// / / / / / /// / / "+id);
      Thread splitthread=new ThreadHome().findById(id);
       
      SplitActionForm sform=new SplitActionForm();
      sform.setOldthread(splitthread);
                
      sform.setOldtitle(splitthread.getTitle());
      sform.setOldthreadid(id);
      sform.setOldthreadbody(splitthread.getBody());
      sform.setOlddescription(splitthread.getThread_description());
       request.setAttribute("SplitActionForm", sform);
        System.out.println("// / / / / / /...  Split Action .......... .. ..   // / / "+sform.getOldthreadbody());
        
        return mapping.findForward("splitpage");
    }
     
     
     
     
     
     
     public ActionForward Execute_Split(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        
      SplitActionForm splitform=(SplitActionForm)form;
      ThreadHome th=new ThreadHome();
      Thread splitthread=th.findById(splitform.getOldthreadid());
         for (int i = 0; i < splitform.getNewpostsid().length; i++) {
             System.out.println("%%%%%%%%%%%%%%%%%%%%%   =  "+splitform.getNewpostsid()[i]);
         }
         
         
       splitthread.setTitle(splitform.getOldtitle());
       splitthread.setBody(splitform.getOldthreadbody());
               
     Member member=new MemberHome().findById(1);   // get fom session
      Thread newthread=new Thread(member, splitthread.getForum(),splitform.getNewtitle() , splitform.getNewthreadbody(), false, false, false, 0, new Date());
      newthread.setThread_description(splitform.getOlddescription());
      String fname=splitthread.getForum().getName();
      Integer fid=splitthread.getForum().getForumId();
              
              
              th.session.close();
      ModeratorManageThreads management=new ModeratorManageThreads();
      management.splitThread(splitthread, newthread, splitform.getNewpostsid());
     
         response.sendRedirect("ViewForum.jsp?forumid="+fid.toString()+"&forumname="+fname);
         return  null;
    }

     
     
     
     
     
     
     
}
